package com.kusen.springsource.aop;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.Arrays;

@Component  //注入到容器中
@Aspect  //定义成一个切面的类
public class LogAspect {
	//前置通知
	@Before("execution(* com.kusen.springsource.aop.HelloService.sayHello(..))")
   public void logStart(JoinPoint joinPoint){
		String name = joinPoint.getSignature().getName();
		System.out.println("logStart()==>"+name+"[args"+ Arrays.asList(joinPoint.getArgs())+"]");
	}
	//返回通知
	@AfterReturning("execution(* com.kusen.springsource.aop.HelloService.sayHello(..))")
	public void logRun(JoinPoint joinPoint){
		String name = joinPoint.getSignature().getName();
		System.out.println("logRun()==>"+name+"[args"+ Arrays.asList(joinPoint.getArgs())+"]");
	}
	//后置通知
	@AfterReturning("execution(* com.kusen.springsource.aop.HelloService.sayHello(..))")
	public void AfterRun(JoinPoint joinPoint){
		String name = joinPoint.getSignature().getName();
		System.out.println("AfterRun()==>"+name+"[args"+ Arrays.asList(joinPoint.getArgs())+"]");
	}
	//异常通知
	@AfterThrowing(value="execution(* com.kusen.springsource.aop.HelloService.sayHello(..))")
	public void afterThrowing (JoinPoint joinPoint){
		String name = joinPoint.getSignature().getName();
		System.out.println("afterThrowing()==>"+name+"[args"+ Arrays.asList(joinPoint.getArgs())+"]");
	}
}
